package com.fstech.whiteboadrd.control;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.fstech.whiteboadrd.R;
import com.fstech.whiteboadrd.data.DataManager;
import com.fstech.whiteboadrd.lib.bean.SketchData;
import com.fstech.whiteboadrd.lib.view.SketchView;
import com.fstech.whiteboadrd.utils.ThreadPoolUtils;
import com.fstech.whiteboadrd.utils.UIUtils;

import java.io.File;
import java.io.FileOutputStream;

import static com.fstech.whiteboadrd.lib.bean.StrokeRecord.STROKE_TYPE_DRAW;
import static com.fstech.whiteboadrd.lib.bean.StrokeRecord.STROKE_TYPE_ERASER;

public class OperateUtil {

    final static String TAG = "OperateUtil";

    public static void addNewPage(SketchView mSketchView) {
        SketchData newSketchData = DataManager.getInstance().addNewSketchData();
        mSketchView.updateSketchData(newSketchData);
        mSketchView.setEditMode(SketchView.EDIT_STROKE);//切换笔画编辑模式
        mSketchView.setVisibility(View.VISIBLE);
        mSketchView.erase();
    }

    public static void clearPage(SketchView mSketchView) {
        mSketchView.erase();
    }

    public static void switchBgColor(SketchView mSketchView, int color) {
//        Drawable drawable = new ColorDrawable(UIUtils.getColor(colorRes));
        Bitmap bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_8888);
        bitmap.eraseColor(color);//填充颜色
        mSketchView.setBackgroundByBitmap(bitmap);
    }

    public static void swichEditType(SketchView mSketchView, boolean isErase) {
        mSketchView.setEditMode(SketchView.EDIT_STROKE);
        mSketchView.setStrokeType(isErase ? STROKE_TYPE_ERASER : STROKE_TYPE_DRAW);
    }

    public static void setEraseSize(SketchView mSketchView, int size) {
        swichEditType(mSketchView, true);
        mSketchView.setSize(size, STROKE_TYPE_ERASER);
    }

    private static void setPenSize(SketchView mSketchView, int size) {
        mSketchView.setSize(size, STROKE_TYPE_DRAW);
    }

    public static void refreshPen(SketchView mSketchView, int size, int color) {
//        swichEditType(mSketchView, false);
        setPenColor(mSketchView, color);
        setPenSize(mSketchView, size);
    }

    public static void setPenColor(SketchView mSketchView, int color) {
        mSketchView.setStrokeColor(color);
    }

    public static void redo(SketchView mSketchView) {
        mSketchView.redo();
    }

    public static void undo(SketchView mSketchView) {
        mSketchView.undo();
    }

    public static boolean isEmpty(SketchView mSketchView) {
        return mSketchView.getRecordCount() <= 0;
    }


    public static String getSaveName(String imgName) {
        return imgName + "--" + System.currentTimeMillis() + ".png";
    }

    public static String getPath(String imgName) {
        return DataManager.FILE_PATH + getSaveName(imgName);
    }

    public static void save(final SketchView mSketchView, final String imgName, final OperateCallback callback) {
        if (isEmpty(mSketchView)) {
            Toast.makeText(mSketchView.getContext(), R.string.empty_tip, Toast.LENGTH_SHORT).show();
            callback.onSaveFailed();
            return;
        }
        callback.onSaveStart();
        ThreadPoolUtils.getInstance().execute(new Runnable() {
            @Override
            public void run() {
                final String saveName = getSaveName(imgName);
                final File file = saveInOI(DataManager.FILE_PATH, saveName, 80, mSketchView);
                UIUtils.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        if (file.exists()) {
                            callback.onSaveSuccess(DataManager.FILE_PATH + imgName + ".png",DataManager.FILE_PATH + saveName + ".png");
                        } else {
                            callback.onSaveFailed();
                        }
                    }
                });
            }
        });
    }

    /**
     * 获取当前白板的BitMap
     */
    public static Bitmap getResultBitmap(SketchView mSketchView) {
        return mSketchView.getResultBitmap();
    }

    public static File saveInOI(String filePath, String imgName, int compress, SketchView mSketchView) {
        if (!imgName.contains(".png")) {
            imgName += ".png";
        }
        Log.e(TAG, "saveInOI: " + System.currentTimeMillis());
        Bitmap newBM = getResultBitmap(mSketchView);
        Log.e(TAG, "saveInOI: " + System.currentTimeMillis());
        try {
            File dir = new File(filePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File f = new File(filePath, imgName);
            if (!f.exists()) {
                f.createNewFile();
            } else {
                f.delete();
            }
            FileOutputStream out = new FileOutputStream(f);
            Log.e(TAG, "saveInOI: " + System.currentTimeMillis());
            if (compress >= 1 && compress <= 100)
                newBM.compress(Bitmap.CompressFormat.PNG, compress, out);
            else {
                newBM.compress(Bitmap.CompressFormat.PNG, 80, out);
            }
            Log.e(TAG, "saveInOI: " + System.currentTimeMillis());
            out.close();
            newBM.recycle();
            newBM = null;
            return f;
        } catch (Exception e) {
            return null;
        }
    }

    public static void jumpToFile(String imagePath, Activity context) {
        //imagePath如 /storage/sdcard1/MIUI/music/mp3_hd/单色冰淇凌_单色凌.mp3
        File file = new File(imagePath);
        //获取父目录
        File parentFlie = new File(file.getParent());
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setDataAndType(Uri.fromFile(parentFlie), "*/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        context.startActivity(intent);
    }

    public interface OperateCallback {
        void onSaveStart();

        void onSaveSuccess(String s, String path);

        void onSaveFailed();
    }

}
